package com.example.demo.threadPool;

import java.util.concurrent.*;

/**
 * @Description:
 * @author: fanfan
 * @date: 2019/3/20 16:26
 */
public class ExecutorWrapper {

    public static ExecutorService newFixedThreadPool(int nThreads) {
        return new ThreadPoolExecutorMdcWrapper(nThreads, nThreads,
                0L, TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue<Runnable>());
    }

    public static ExecutorService newFixedThreadPool(int nThreads, ThreadFactory threadFactory) {
        return new ThreadPoolExecutorMdcWrapper(nThreads, nThreads,
                0L, TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue<Runnable>(),
                threadFactory);
    }

    public static ExecutorService newWorkStealingPool(int parallelism) {
        return new ForkJoinPoolMdcWrapper
                (parallelism,
                        ForkJoinPool.defaultForkJoinWorkerThreadFactory,
                        null, true);
    }

    public static ExecutorService newWorkStealingPool() {
        return new ForkJoinPoolMdcWrapper
                (Runtime.getRuntime().availableProcessors(),
                        ForkJoinPool.defaultForkJoinWorkerThreadFactory,
                        null, true);
    }

    public static ExecutorService newCachedThreadPool() {
        return new ThreadPoolExecutorMdcWrapper(0, Integer.MAX_VALUE,
                60L, TimeUnit.SECONDS,
                new SynchronousQueue<Runnable>());
    }

    public static ExecutorService newCachedThreadPool(ThreadFactory threadFactory) {
        return new ThreadPoolExecutorMdcWrapper(0, Integer.MAX_VALUE,
                60L, TimeUnit.SECONDS,
                new SynchronousQueue<Runnable>(),
                threadFactory);
    }
}
